package com.bocai.kelalacar.ui.driver.set.act;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.bocai.kelalacar.App;
import com.bocai.kelalacar.R;
import com.bocai.kelalacar.base.BaseActivity;
import com.bocai.kelalacar.bean.TravelInformationBean;
import com.bocai.kelalacar.bean.UploadImgBean;
import com.bocai.kelalacar.ui.car.main.act.MainActivity;
import com.bocai.kelalacar.ui.car.main.act.SpaceImageDetailActivity;
import com.bocai.kelalacar.ui.car.set.act.CertificationInformationActivity;
import com.bocai.kelalacar.ui.car.set.act.CertificationInformationStateActivity;
import com.bocai.kelalacar.ui.car.set.ada.CarPhotoAda;
import com.bocai.kelalacar.ui.driver.set.contract.DriverCertificationContract;
import com.bocai.kelalacar.ui.driver.set.module.DriverCertificationModule;
import com.bocai.kelalacar.ui.driver.set.presenter.DriverCertificationPresenter;
import com.bocai.kelalacar.util.SP;
import com.bocai.kelalacar.widget.MyGridView;
import com.bocai.kelalacar.widget.RoundedImageView;
import com.bumptech.glide.Glide;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by yuanfei on 2017/6/26.
 */

public class DriverCertificationInformationStateActivity extends BaseActivity implements DriverCertificationContract.view {
    @BindView(R.id.img_back)
    ImageView imgBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.right_btn)
    Button rightBtn;
    @BindView(R.id.tv_right)
    TextView tvRight;
    @BindView(R.id.ll_right)
    LinearLayout llRight;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.tv_phone)
    TextView tvPhone;
    @BindView(R.id.ll_type1)
    LinearLayout llType1;
    @BindView(R.id.btn_again_submit)
    Button btnAgainSubmit;
    @BindView(R.id.ll_type2)
    LinearLayout llType2;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.tv_id_card)
    TextView tvIdCard;
    @BindView(R.id.tv_address_phone)
    TextView tvAddressPhone;
    @BindView(R.id.iv_driving_license)
    RoundedImageView ivDrivingLicense;
    @BindView(R.id.iv_jiashi_license)
    RoundedImageView ivJiashiLicense;
    @BindView(R.id.iv_transport_license)
    RoundedImageView ivTransportLicense;
    @BindView(R.id.iv_transport_certificate)
    RoundedImageView ivTransportCertificate;
    @BindView(R.id.iv_insurance_card)
    RoundedImageView ivInsuranceCard;
    @BindView(R.id.textView3)
    TextView textView3;
    @BindView(R.id.ed_license_plate)
    TextView edLicensePlate;
    @BindView(R.id.tv_car_age)
    TextView tvCarAge;
    @BindView(R.id.tv_car_num)
    TextView tvCarNum;
    @BindView(R.id.grid_car)
    GridView gridCar;
    @BindView(R.id.tv_invoice_num)
    TextView tvInvoiceNum;
    @BindView(R.id.tv_customer_service_phone)
    TextView tvCustomerServicePhone;
    @BindView(R.id.scroll_type3)
    ScrollView scrollType3;
    CarPhotoAda carPhotoAda;
    @BindView(R.id.ll_back)
    LinearLayout llBack;

    @Inject
    DriverCertificationPresenter presenter;
    private ArrayList<TravelInformationBean.InfomationBean.Imagebean> list = new ArrayList<>();

    @Override
    public int getLayoutId() {
        return R.layout.act_diver_cer_infor_state;
    }

    @Override
    public void initPresenter() {
        App.get(this).getAppComponent().addSub(new DriverCertificationModule(this)).inject(this);
    }

    @Override
    public void initView() {
        initToolbar("认证信息");
        initBack(imgBack,llBack);
        gridCar.setFocusable(false);
        presenter.getTravelInfor(SP.get(this,"id","").toString());
        carPhotoAda = new CarPhotoAda(this,list);
        gridCar.setAdapter(carPhotoAda);
        btnAgainSubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(DriverCertificationInformationActivity.class);
            }
        });
    }

    @Override
    public void putDriverInforSuccess() {

    }

    @Override
    public void uploadImgSuccess(UploadImgBean uploadImgBean) {

    }

    @Override
    public void getTravelSuccess(final TravelInformationBean bean) {

        SP.put(this,"ApprovalStatus",bean.getApprovalStatus());
        if (bean.getApprovalStatus() == -1){
            llType2.setVisibility(View.VISIBLE);
        }else if (bean.getApprovalStatus() == 1){
            llType1.setVisibility(View.VISIBLE);
        }else if (bean.getApprovalStatus() == 2){
            scrollType3.setVisibility(View.VISIBLE);
            tvName.setText(bean.getInfomation().getContactName());
            String bb = bean.getInfomation().getContactPhone().replaceAll("(\\d{3})\\d{4}(\\d{4})", "$1****$2");
            String aa = bean.getInfomation().getIdNumber().replaceAll("(\\d{6})\\d{8}(\\d{4})", "$1********$2");
            tvIdCard.setText(aa);
            tvAddressPhone.setText(bb);

            Glide.with(this).load(bean.getInfomation().getDrivingLicense().getMediumThumbnail()).into(ivDrivingLicense);
            Glide.with(this).load(bean.getInfomation().getDriverLicense().getMediumThumbnail()).into(ivJiashiLicense);
            Glide.with(this).load(bean.getInfomation().getTransportLicense().getMediumThumbnail()).into(ivTransportLicense);
            Glide.with(this).load(bean.getInfomation().getQualificationsLicense().getMediumThumbnail()).into(ivTransportCertificate);
            Glide.with(this).load(bean.getInfomation().getInsuranceCard().getMediumThumbnail()).into(ivInsuranceCard);
            edLicensePlate.setText(bean.getInfomation().getCarLicense());
            tvCarAge.setText(bean.getInfomation().getCarAge()+"年");
            tvCarNum.setText(bean.getInfomation().getSeats()+"座");
            carPhotoAda.setPhotos(bean.getInfomation().getCarImages());

            ivDrivingLicense.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(DriverCertificationInformationStateActivity.this, SpaceImageDetailActivity.class);
                    intent.putExtra("imgUrl", bean.getInfomation().getDrivingLicense().getMediumThumbnail());
//                    intent.putExtra("position", position);
                    int[] location = new int[2];
                    ivDrivingLicense.getLocationOnScreen(location);
                    intent.putExtra("locationX", location[0]);
                    intent.putExtra("locationY", location[1]);

                    intent.putExtra("width", ivDrivingLicense.getWidth());
                    intent.putExtra("height", ivDrivingLicense.getHeight());
                    startActivity(intent);
                    overridePendingTransition(0, 0);
                }
            });
            ivJiashiLicense.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(DriverCertificationInformationStateActivity.this, SpaceImageDetailActivity.class);
                    intent.putExtra("imgUrl", bean.getInfomation().getDriverLicense().getMediumThumbnail());
//                    intent.putExtra("position", position);
                    int[] location = new int[2];
                    ivJiashiLicense.getLocationOnScreen(location);
                    intent.putExtra("locationX", location[0]);
                    intent.putExtra("locationY", location[1]);

                    intent.putExtra("width", ivJiashiLicense.getWidth());
                    intent.putExtra("height", ivJiashiLicense.getHeight());
                    startActivity(intent);
                    overridePendingTransition(0, 0);
                }
            });
            ivTransportLicense.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(DriverCertificationInformationStateActivity.this, SpaceImageDetailActivity.class);
                    intent.putExtra("imgUrl", bean.getInfomation().getTransportLicense().getMediumThumbnail());
//                    intent.putExtra("position", position);
                    int[] location = new int[2];
                    ivTransportLicense.getLocationOnScreen(location);
                    intent.putExtra("locationX", location[0]);
                    intent.putExtra("locationY", location[1]);

                    intent.putExtra("width", ivTransportLicense.getWidth());
                    intent.putExtra("height", ivTransportLicense.getHeight());
                    startActivity(intent);
                    overridePendingTransition(0, 0);
                }
            });
            ivTransportCertificate.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(DriverCertificationInformationStateActivity.this, SpaceImageDetailActivity.class);
                    intent.putExtra("imgUrl", bean.getInfomation().getQualificationsLicense().getMediumThumbnail());
//                    intent.putExtra("position", position);
                    int[] location = new int[2];
                    ivTransportCertificate.getLocationOnScreen(location);
                    intent.putExtra("locationX", location[0]);
                    intent.putExtra("locationY", location[1]);

                    intent.putExtra("width", ivTransportCertificate.getWidth());
                    intent.putExtra("height", ivTransportCertificate.getHeight());
                    startActivity(intent);
                    overridePendingTransition(0, 0);
                }
            });
            ivInsuranceCard.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(DriverCertificationInformationStateActivity.this, SpaceImageDetailActivity.class);
                    intent.putExtra("imgUrl", bean.getInfomation().getInsuranceCard().getMediumThumbnail());
//                    intent.putExtra("position", position);
                    int[] location = new int[2];
                    ivInsuranceCard.getLocationOnScreen(location);
                    intent.putExtra("locationX", location[0]);
                    intent.putExtra("locationY", location[1]);

                    intent.putExtra("width", ivInsuranceCard.getWidth());
                    intent.putExtra("height", ivInsuranceCard.getHeight());
                    startActivity(intent);
                    overridePendingTransition(0, 0);
                }
            });

        }else {
            startActivity(DriverCertificationInformationActivity.class);
            finish();
        }
    }

    @Override
    public void optionalSeatsList(List<Integer> list) {

    }

    @Override
    public void showLoading(String title) {
        startProgressDialog(title);
    }

    @Override
    public void stopLoading() {
        stopProgressDialog();
    }

    @Override
    public void showErrorTip(String msg) {
        showShortToast(msg);
        if (msg.equals("您的登录已失效，请重新登录")){
            startActivity(new Intent(this,MainActivity.class).putExtra("index",1));
            finish();
        }
    }
}
